from django.db import models

# Create your models here.
from django.db import models


class Menu(models.Model):
    title = models.CharField(verbose_name='菜单名', max_length=32)
    icon = models.CharField(verbose_name='菜单图标', null=True, blank=True, max_length=32)


class Permission(models.Model):
    """
    权限表
    """
    title = models.CharField(verbose_name='标题', max_length=32)
    url = models.CharField(verbose_name='含正则的URL', max_length=128)
    # is_menu = models.BooleanField(verbose_name='是否作为菜单',default=False,blank=True)
    # icon = models.CharField(verbose_name='菜单图标',null=True,blank=True,max_length=32)
    alias = models.CharField(verbose_name="给url 设置唯一别名", unique=True, max_length=32, null=True)
    menu = models.ForeignKey(to='Menu', verbose_name='关联菜单Id', blank=True, null=True, on_delete=models.CASCADE)
    pid = models.ForeignKey(to='Permission', blank=True, null=True, on_delete=models.CASCADE)

    def __str__(self):
        return self.title


class Role(models.Model):
    """
    角色
    """
    title = models.CharField(verbose_name='角色名称', max_length=32)
    permissions = models.ManyToManyField(verbose_name='拥有的所有权限', to='Permission', blank=True)

    def __str__(self):
        return self.title


class UserInfo(models.Model):
    """
    用户表
    """
    name = models.CharField(verbose_name='用户名', max_length=32)
    password = models.CharField(verbose_name='密码', max_length=64)
    email = models.CharField(verbose_name='邮箱', max_length=32)
    roles = models.ManyToManyField(verbose_name='拥有的所有角色', to='Role', blank=True)

    def __str__(self):
        return self.name

    # class Meta:
    #     # django以后再做数据库迁移时，不再为UserInfo类创建相关的表以及表结构了。
    #     # 此类可以当做"父类"，被其他Model类继承。
    #     abstract = True
